﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.yjb;
using B.S.Sterilizationcenter.Domain.zmc;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Yjb.ConsumableMaterials;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Yjb.ConsumableMaterials
{
    public class UpdateConsumableMaterialCommandHandlers : IRequestHandler<UpdateConsumableMaterialCommand, ApiResult<int>>
    {

        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public UpdateConsumableMaterialCommandHandlers(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(UpdateConsumableMaterialCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var data = mapper.Map<ConsumableMaterial>(request);

            result.Data = await db.Updateable(data).ExecuteCommandAsync();
            if (result.Data > 0)
            {
                result.Code = ApiEnums.修改成功;
                result.Msg = "修改成功";

            }
            else
            {
                result.Code = ApiEnums.修改失败;
                result.Msg = "修改失败";
            }
            return result;
        }
    }
}
